###############################################################################
### Description: 	This document provides the code for reproducing the 	    ###
###					power analysis in the Supporting Information for the paper, 	  ###	
###					"Does Compulsory Voting Affect How Voters Choose? A 	          ###	
###					Test Using a Combined Conjoint and Regression 			            ###	
###					Discontinuity Analysis," which is authored by Shane P.        	###	
###					Singh and appears in Comparative Political Studies.	          	###	
###         It also provides the code for reproducing statistics            ###         
###         associated with claims made in the text.                        ###         
###         It also includes the code for exporting the data that is used	  ###	
###         to create Figure SM24 in	                                      ###	
###         "Replication, Supporting Information.do"	                      ###	
###############################################################################

##############
##############
#Install Required Packages                                                                                                                      
##############
##############
if(!require(devtools)) install.packages("devtools")
library(devtools)
devtools::install_github("m-freitag/cjpowR")

library(cjpowR)


##############
##############
#Do the Power Analysis as Effective Sample Size Changes for Various Effect Size Differences                                                                                                                
##############
##############
cjpowr_amcie_vec <- Vectorize(cjpowr_amcie)

d <- expand.grid(delta3 = c(.08, .09, 0.10, .11, .12), n = seq(from = 100, to = 15000, length.out = 100))

df <- t(cjpowr_amcie_vec(delta3 = d$delta3, n = d$n, levels1 = 3.875, levels2=2, alpha = 0.05))

df <- data.frame(df)

df[] <- lapply(df, unlist)


###
#Export the data to graph in Stata. Be sure to save it to the working directory you are using in Stata. 
###
write.csv(df, file = 'Figure_SI24_data.csv')


##############
##############
#Evidence for Claim Made in the Text: "At this benchmark [difference of 0.10 in effect sizes], the required effective sample size to 
#obtain 80% power... is 6,022."
##############
##############
cjpowr_amcie(power = .8,delta3  = .1, levels1 = 3.875, levels2 = 2, alpha =0.05)


##############
##############
#Evidence for Claim Made in the Text: "For the analyses of the younger age group, statistical power to detect the benchmark difference is 0.982."
##############
##############
cjpowr_amcie(delta3  = .1, levels1 = 3.875, levels2 = 2, alpha =0.05, n=12650)


##############
##############
#Evidence for Claim Made in the Text: "For the analyses of the older age group, statistical power to detect the benchmark difference is 0.889."
##############
##############
cjpowr_amcie(delta3  = .1, levels1 = 3.875, levels2 = 2, alpha =0.05, n=7770)


##############
##############
#Evidence for Claim Made in the Text: "When I account for these imbalances in the power calculations, results remain very similar: 
#for the younger group, statistical power is 0.972; for the older group, it is 0.867."
##############
##############
#If 64.82213% in one of the two conditions and 35.17787% in the other, as is the case with the tests for the younger group
cjpowr_amcie(delta3  = .1, levels1 = 3.875, levels2 = 2, alpha =0.05, n=12650, treat.prob = c(0.5*.6482213, 0.5*.6482213, 0.5*.3517787, 0.5*.3517787))

#If 37.32304% in one of the two conditions and 62.67696% in the other, as is the case with the tests for the older group
cjpowr_amcie(delta3  = .1, levels1 = 3.875, levels2 = 2, alpha =0.05, n=7770, treat.prob = c(0.5*.3732304, 0.5*.3732304, 0.5*.6267696, 0.5*.6267696))



